package org.huangrui.spark.scala.core.rdd.operate.transform

import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author hr
 * @Create 2024-10-17 18:14 
 */
object Spark12_Operate_Transform_KV_ReduceBykey {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
//    conf.setMaster("local[*]")
    conf.setAppName("spark")
    val sc = new SparkContext(conf)
    val tuples = List(("a", 1), ("b", 4), ("a", 3), ("b", 2))

    // TODO 将分组聚合功能进行简化操作
    //     reduceByKey方法的作用：将KV类型的数据按照 K 对 V 进行reduce（将多个值聚合成一个值）操作
    //         [1,2,3,4] => 10
    //     计算的基本思想：两两计算
    //     (i1, i2) => i3
    sc.parallelize(tuples, 2).reduceByKey(_ + _).collect().foreach(println)
    sc.stop()
  }
}
